<template>
  <div class="vk-divide-list" :style="{ backgroundColor: backgroundColor }">
    <div
      class="vk-divide-list-item"
      v-for="(item, index) in list"
      :key="index"
      :style="{
        padding: `${padding}px`,
      }"
      @click="itemClick(index)"
    >
      <div class="vk-divide-list-item-value">
        <!-- 值 -->
        <div
          class="item-value"
          :style="{
            color: valueColor,
            size: valueSize,
            fontWeight: valueFontWeight,
          }"
          v-if="item[valueField] && !item[srcField]"
        >
          {{ item[valueField] }}
        </div>
        <!-- 图片 -->
        <image
          :src="item[srcField]"
          :style="{
            width: (item.width || width) + 'rpx',
            height: (item.height || height) + 'rpx',
          }"
          v-if="item[srcField]"
        >
        </image>

        <!-- 角标 -->
        <div
          :class="item.isDot ? 'tui-badge-dot' : 'tui-badge'"
          :style="{
            color: badgeColor,
            backgroundColor: badgeBgColor,
          }"
          v-if="item[numField] > 0 || item.isDot"
        >
          {{ item[numField] > 0 && !item.isDot ? item[numField] : "" }}
        </div>
      </div>

      <!-- 描述 -->
      <div
        class="vk-divide-list-item-text"
        :style="{
          color: textColor,
          size: textColor,
          fontWeight: textFontWeight,
        }"
      >
        {{ item[textField] }}
      </div>
      <!-- 分隔线 -->
      <div
        class="vk-divide-list-item-line"
        v-if="divider && index < list.length - 1"
        :style="{ backgroundColor: dividerColor, height: dividerHeight + '%' }"
      ></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "VkDivideList",
  props: {
    list: {
      type: Array,
      default: () => [],
    },
    // 图片宽度
    width: {
      type: Number,
      default: 300,
    },
    // 图片高度
    height: {
      type: Number,
      default: 40,
    },
    // text指定的key
    textField: {
      type: String,
      default: "text",
    },
    // value指定的key
    valueField: {
      type: String,
      default: "value",
    },
    // src指定的key
    srcField: {
      type: String,
      default: "src",
    },
    // num指定的key
    numField: {
      type: String,
      default: "num",
    },
    textSize: {
      type: [Number, String],
      default: 24,
    },
    textColor: {
      type: String,
      default: "#999",
    },
    textFontWeight: {
      type: [Number, String],
      default: 400,
    },
    valueSize: {
      type: [Number, String],
      default: 32,
    },
    valueColor: {
      type: String,
      default: "#333",
    },
    valueFontWeight: {
      type: [Number, String],
      default: 400,
    },
    padding: {
      type: [Number, String],
      default: 20,
    },
    // 背景颜色
    backgroundColor: {
      type: String,
      default: "#f5f5f5",
    },
    // 边框颜色
    borderColor: {
      type: String,
      default: "#e5e5e5",
    },
    //是否显示分割线
    divider: {
      type: Boolean,
      default: true,
    },
    // 分割线颜色
    dividerColor: {
      type: String,
      default: "#e5e5e5",
    },
    //分割线百分比：1~100
    dividerHeight: {
      type: [Number, String],
      default: 60,
    },
    //角标字体颜色
    badgeColor: {
      type: String,
      default: "#fff",
    },
    //角标背景颜色
    badgeBgColor: {
      type: String,
      default: "",
    },
    //角标数字，大于0时显示，可使用属性numField指定key
    num: 0,
    //角标圆点，为true时显示圆点，num失效
    isDot: false,
  },
  data() {
    return {};
  },
  methods: {
    itemClick(index) {
      this.$emit("click", index);
    },
  },
};
</script>

<style scoped>
.vk-divide-list {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.vk-divide-list-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  position: relative;
}

.vk-divide-list-item-value {
  position: relative;
}

.tui-badge-dot {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff0000;
}

.tui-badge {
  position: absolute;
  top: -6px;
  right: -12px;
  border-radius: 50%;
  background: #ff0000;
  padding: 2px;
  font-size: 8px;
  height: 12px;
  line-height: 12px;
  min-width: 12px;
  padding: 1px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.vk-divide-list-item-line {
  position: absolute;
  right: 0;
  width: 1px;
  height: 100%;
  background-color: "#e5e5e5";
}
</style>
